package com.apes.fn.scm.lossSale;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.stock.model.Location;
import com.apes.scm.sal.saleOrder.SaleOrderItem;
import com.apes.scm.sal.saleOrder.SaleOrderItemRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * @Author wuq
 * @Date 2021-12-16
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LossSaleReturnItem.CONST_TABLE_NAME)
public class LossSaleReturnItem extends BaseModel {
    public final static String CONST_TABLE_NAME = "loss_Sale_return_Item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "退货明细行")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name="sale_return_id", referencedColumnName = "id", nullable = false)
    private LossSaleReturn lossSaleReturn;

    @Column(name = "sale_item_id", length = 30)
    @Comment("分销单明细行号")
    private String saleItemId;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("库存位置")
    private Location location;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    @Comment("商品")
    private Product product;

    @Column(name = "qty_sale", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("销售数量")
    private double qtySaleQty = 0;

    @Column(name = "qty_delivered", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("退货数量")
    private double qtyDelivered = 0;

    @Column(name = "qty_received", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("收货数量")
    private double qtyReceived = 0;

    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("单价")
    private double priceUnit = 0;

    @Column(name = "price_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总计")
    private double priceTotal = 0;

    @Column(name = "remark", length = 120)
    @Comment("备注")
    private String remark;

    @Transient
    private String uomName;
    public String getUomName(){
        if (this.product == null) return "";
        return this.product.getUom().getName();
    }

    @Transient
    private double qtyDeliverable = 0;  // 应该退的部分，可退的部分

    public double getQtyDeliverable(){
        if (StringUtils.isEmpty(this.saleItemId)) return 0;
        SaleOrderItem saleOrderItem = SpringManager.getBean(SaleOrderItemRepository.class).findOne(this.saleItemId);
        return saleOrderItem.calcQtyDeliveredLimit();
    }

    @Transient
    private double qtyReturning = 0;    // 退货中数量

    public double getQtyReturning(){
        if (StringUtils.isEmpty(this.saleItemId)) return 0;

        SaleOrderItem saleOrderItem = SpringManager.getBean(SaleOrderItemRepository.class).findOne(this.saleItemId);
        return saleOrderItem.getQtyReturning();
    }

    public void delivery(double qtyDone) {
        this.qtyReceived += qtyDone;
    }
}
